package com.light.generator.handler;

import com.light.common.utils.SuperStringUtils;
import com.light.generator.domain.ColumnDO;
import com.light.generator.domain.TableDO;
import com.light.generator.exception.GeneratorException;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Tittle TableHandler
 * @Description
 * @Author yhr
 * @Date 2019/4/3 13:26
 * @Version 1.0
 */
public class GeneratorHandler {

    public ColumnDO test() {
        return new ColumnDO();
    }

    public TableDO testTable() {
        return new TableDO();
    }

    private Map<String, String> table;
    private List<Map<String, String>> columns;
    private Configuration config;

    public GeneratorHandler(Map<String, String> table,List<Map<String, String>> columns) {
        this.table = table;
        this.columns = columns;
        config = getGeneratorConfig();
    }
//
    public TableDO handle(String tablePrefix) {
        // 表信息
        TableDO tableDO = createTableInfo(tablePrefix);

        // 列信息
        List<ColumnDO> columsList = new ArrayList<>();
        for (Map<String, String> column : columns) {
            ColumnDO columnDO = new ColumnDO();
            columnDO.setColumnName(column.get("columnName"));
            columnDO.setDataType(SuperStringUtils.upperCase(column.get("dataType")));
            columnDO.setComments(column.get("columnComment"));
            columnDO.setExtra(column.get("extra"));
            // 列名转换成Java属性名
            String attrName = SuperStringUtils.humpNaming(columnDO.getColumnName());
            columnDO.setAttrName(attrName);
            columnDO.setAttrNameLowerCase(StringUtils.uncapitalize(attrName));
            //列的数据类型，转换成Java类型
            String attrType = config.getString(columnDO.getDataType().toLowerCase(), "unknowType");
            columnDO.setAttrType(attrType);
            // 是否主键
            if (checkIsPrimaryKey(tableDO, column)) {
                tableDO.setPk(columnDO);
            }
            columsList.add(columnDO);
        }
        tableDO.setColumns(columsList);
        // 没主键，则第一个字段为主键
        if (tableDO.getPk() == null) {
            throw new GeneratorException("该表未设置主键!表名：" + tableDO.getTableName());
        }

        return tableDO;
    }
//
    private TableDO createTableInfo(String tablePrefix) {
        TableDO tableDO = new TableDO();
        tableDO.setTableName(table.get("tableName"));
        tableDO.setComments(table.get("tableComment"));
        // 表名转换成Java类名
        String className = tableToJava(tableDO.getTableName(), tablePrefix);
        tableDO.setClassName(className);
        tableDO.setClassNameLowerCase(SuperStringUtils.uncapitalize(className));
        return tableDO;
    }

    private boolean checkIsPrimaryKey(TableDO tableDO, Map<String, String> column) {
        return "PRI".equalsIgnoreCase(column.get("columnKey")) && tableDO.getPk() == null;
    }

    public String tableToJava(String tableName, String tablePrefix) {
        if (SuperStringUtils.isNotBlank(tablePrefix)) {
            tableName = tableName.replace(tablePrefix, "");
        }
        return SuperStringUtils.humpNaming(tableName);
    }
//
    public Configuration getGeneratorConfig() {
        try {
            return new PropertiesConfiguration("generator.properties");
        } catch (ConfigurationException e) {
            throw new GeneratorException("获取配置文件失败，");
        }
    }
}
